/**
 * 
 * Copyright (c) 2009, Apptude Limited
 * License: New BSD License see license.txt
 * http://www.opensource.org/licenses/bsd-license.php
 * 
 */

package com.apptude.android.trivial.gpswatch;

import android.app.Activity;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class GpsWatch extends Activity implements GpsStatus.Listener,
		LocationListener
{
	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		try
		{
			super.onCreate(savedInstanceState);

			setContentView(R.layout.main);

			if (null == locationView)
			{
				locationView = (LocationView) findViewById(R.id.location_view);
			}

			if (null == signalStrengthView)
			{
				signalStrengthView = (SignalStrengthView)findViewById(R.id.signal_strength_view);
			}
			if (null == locationManager)
			{
				locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
			}
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			Log.e(TAG, e.getClass().toString());
			Log.e(TAG, e.getMessage());
		}
	}

	@Override
	protected void onRestart()
	{
		try
		{
			Log.v(TAG, ">>>> " + getClass() + " onRestart");

			super.onRestart();

			Log.v(TAG, "<<<< " + getClass() + " onRestart");
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			Log.e(TAG, e.getClass().toString());
			Log.e(TAG, e.getMessage());
		}
	}

	@Override
	protected void onStart()
	{
		try
		{
			Log.v(TAG, ">>>> " + getClass() + " onStart");

			super.onStart();

			Log.v(TAG, "<<<< " + getClass() + " onStart");
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			Log.e(TAG, e.getClass().toString());
			Log.e(TAG, e.getMessage());
		}
	}

	@Override
	protected void onResume()
	{
		try
		{
			Log.v(TAG, ">>>> " + getClass() + " onResume");

			super.onResume();

			locationManager.addGpsStatusListener(this);
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0.0f, this);

			Log.v(TAG, "<<<< " + getClass() + " onResume");
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			Log.e(TAG, e.getClass().toString());
			Log.e(TAG, e.getMessage());
		}
	}

	@Override
	protected void onPause()
	{
		try
		{
			Log.v(TAG, ">>>> " + getClass() + " onPause");

			super.onPause();
			
			locationManager.removeGpsStatusListener(this);
			locationManager.removeUpdates(this);

			Log.v(TAG, "<<<< " + getClass() + " onPause");
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			Log.e(TAG, e.getClass().toString());
			Log.e(TAG, e.getMessage());
		}
	}

	@Override
	protected void onStop()
	{
		try
		{
			Log.v(TAG, ">>>> " + getClass() + " onStop");

			super.onStop();

			Log.v(TAG, "<<<< " + getClass() + " onStop");
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			Log.e(TAG, e.getClass().toString());
			Log.e(TAG, e.getMessage());
		}
	}

	@Override
	protected void onDestroy()
	{
		try
		{
			Log.v(TAG, ">>>> " + getClass() + " onDestroy");

			super.onDestroy();

			Log.v(TAG, "<<<< " + getClass() + " onDestroy");
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			Log.e(TAG, e.getClass().toString());
			Log.e(TAG, e.getMessage());
		}
	}

	public void onGpsStatusChanged(int event)
	{
		try
		{
			// Log.v(TAG, ">>>> " + getClass() +" onGpsStatusChanged");

			switch (event)
			{
				case GpsStatus.GPS_EVENT_FIRST_FIX:
				{
				}
				break;

				case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
				{
				}
				break;

				case GpsStatus.GPS_EVENT_STARTED:
				{
				}
				break;

				case GpsStatus.GPS_EVENT_STOPPED:
				{
				}
				break;

				default:
				{
				}
				break;
			}

			gpsStatus = locationManager.getGpsStatus(gpsStatus);
			locationView.gpsStatusChanged(gpsStatus);
			signalStrengthView.gpsStatusChanged(gpsStatus);

			// Log.v(TAG, "<<<< " + getClass() +" onGpsStatusChanged");
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			Log.e(TAG, e.getClass().toString());
			Log.e(TAG, e.getMessage());
		}
	}

	public void onLocationChanged(Location location)
	{
		myLocation = location;
	}

	public void onProviderDisabled(String provider)
	{
	}

	public void onProviderEnabled(String provider)
	{
	}

	public void onStatusChanged(String provider, int status, Bundle extras)
	{
	}

	private static final String	TAG	= "GpsWatch";

	private LocationManager		locationManager;
	private Location			myLocation;
	private LocationView		locationView;
	private SignalStrengthView	signalStrengthView;
	private GpsStatus			gpsStatus;

}